<template>
  <div class="dashboard-container">
    <div class="app-container">
      <pageTools>
        <template #after>
          <el-button type="primary" size="small" @click="addFn">添加角色</el-button>
        </template>
      </pageTools>
      <!-- 角色列表 -->
      <el-card style="margin-top:20px">
        <el-table :data="rolesList">
          <el-table-column label="角色名称" prop="title" />
          <el-table-column label="角色描述" prop="description" />
          <el-table-column label="操作">
            <template v-slot="{row}">
              <el-button type="primary" size="small" @click="assignPerm(row.id)">分配权限</el-button>
              <el-button size="small" @click="editFn(row.id)">编辑</el-button>
              <el-button type="danger" size="small" @click="delFn(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- 添加/编辑角色组件 -->
      <detai ref="detai" :show-dialog.sync="showDialog" @updateEvent="getRoleList" />
      <assignPerm ref="assignPerm" :show-dialog.sync="permDialog" />
    </div>
  </div>
</template>

<script>
import { getAllRole, getRoleInfoById, deleteRole } from '@/api/setting'
import detai from './components/detai.vue'
import assignPerm from './components/assignPerm.vue'
export default {
  name: 'ProjectBackIndex',
  components: { detai, assignPerm },

  data() {
    return {
      rolesList: [],
      limit: 999,
      start: 0,
      showDialog: false,
      permDialog: false
    }
  },

  mounted() {

  },
  created() {
    this.getRoleList()
  },
  methods: {
    // 分配权限
    assignPerm(id) {
      this.permDialog = true
      this.$refs.assignPerm.getRolePermId(id)
    },
    // 删除角色
    delFn(id) {
      this.$confirm('您确定删除吗？', '提示', {
        type: 'warning'
      }).then(async() => {
        await deleteRole(id)
        this.getRoleList()
      }).catch(err => {
        console.log(err, '取消')
      })
    },
    // 编辑角色
    async editFn(id) {
      // console.log(id)
      this.showDialog = true
      this.$refs.detai.formData = await getRoleInfoById(id)
      console.log(56, this.$refs.detai.formData)
    },
    // 添加角色
    addFn() {
      this.showDialog = true
    },
    // 获取角色列表
    async getRoleList() {
      const res = await getAllRole({
        _limit: this.limit,
        _start: this.start
      })
      this.rolesList = res
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
